#!/usr/bin/env python
import util

from huffman_enc import *
from runlen_enc import *
from bwt_enc import *

def reach_segment_end():
    global plaincode, syms
    total = len(plaincode)
    sym_dist = []
    for (k,v) in syms.items():
        sym_dist.append((1.0 * v / total, k))
    #print sym_dist
    huffTree = makeHuffTree(sym_dist)
    code = {}
    getHuffCode(huffTree, code)
    print "#huffman code = ", repr(code)
    size_compressed = 0
    for c in plaincode:
        size_compressed += len(code[c])
    print "#num of records =", total
    print "#num of symbols = ", len(syms)
    print "#huffman compression: %f bits/record"%(1.0 * size_compressed / len(plaincode))
    print "#entropy = %f bits/record"%(util.entropy(syms))
    print
    print

    
        

if __name__ == "__main__":
    plaincode = []
    syms = {}
    for l in sys.stdin:
        l = l.strip()
        if l == "":
            # end of a segment
            #reach_segment_end()
            plaincode = []
            syms = {}
            continue

        if l.startswith("#"):
            print l
            continue

        plaincode.append(l)
        try:
            syms[l] += 1
        except:
            syms[l] = 1
    #reach_segment_end()
    segsize = 512
    inputcode = plaincode[:segsize]
    bwt_code = bwt(inputcode)
    #print bwt_code
    runlen_code = runlen(bwt_code)
    #print runlen_code

    runlen_code2 = runlen(inputcode)
    #print runlen_code2
